﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace PlanTasks.Commons
{
    public class PlanTaskManagerConfig
    {
        private readonly XmlDocument _ConfigXml = new XmlDocument();
        private static PlanTaskManagerConfig _PlanTaskManagerConfig = null;
        public readonly List<string> TaskState;//任务状态
        public readonly List<string> TaskType;//任务类型
        public string SyncPath;//任务文件保存地址
        public bool Minimize;//窗口关闭时最小化还是直接关闭

        private PlanTaskManagerConfig()
        {
            _ConfigXml.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PlanTaskManagerConfig.xml"));
            TaskState = new List<string>();
            TaskType = new List<string>();
            TaskState = FreshList("TaskState");
            TaskType = FreshList("TaskType");
            SyncPath = GetSyncPath("SyncPath");
            Minimize = GetMinimize("Minimize");
        }

        public static PlanTaskManagerConfig GetObject()
        {
            if (_PlanTaskManagerConfig == null)
                _PlanTaskManagerConfig = new PlanTaskManagerConfig();
            return _PlanTaskManagerConfig;
        }

        /// <summary>
        /// 获取点击X关闭时软件最小化还是直接关闭的参数
        /// </summary>
        /// <param name="minimize">Minimize节点为0时直接关闭，为其他值时最小化。</param>
        /// <returns></returns>
        private bool GetMinimize(string minimize)
        {
            var mini = "";
            var srcElement = _ConfigXml.DocumentElement?.SelectSingleNode(minimize);
            if (srcElement != null) mini = srcElement.InnerText;
            if (mini.Equals("0")) return false;
            return true;
        }

        /// <summary>
        /// 变更最小化参数
        /// </summary>
        /// <param name="minimize">true代表最小化，false代表直接关闭。</param>
        public void ChangeMinimize(bool minimize)
        {
            if (this.Minimize == minimize)
                return;
            this.Minimize = minimize;

            XmlElement nc = (XmlElement)_ConfigXml.SelectSingleNode("//PlanTaskManagerConfig//Minimize");
            if (nc != null)
            {
                nc.InnerText = minimize ? "1" : "0";
            }
            _ConfigXml.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PlanTaskManagerConfig.xml"));
        }

        private List<string> FreshList(string xpath)
        {
            var map = new List<string>();
            var srcElement = _ConfigXml.DocumentElement?.SelectSingleNode(xpath);
            if (srcElement != null)
            {
                foreach (XmlElement ele in srcElement.ChildNodes)
                {
                    var text = ele.InnerText;
                    map.Add(text);
                }
            }
            return map;
        }

        private string GetSyncPath(string syncpath)
        {
            var path = "";
            var srcElement = _ConfigXml.DocumentElement?.SelectSingleNode(syncpath);
            if (srcElement != null) path = srcElement.InnerText;
            return path;
        }

        public void ChengeSyncPath(string newPath)
        {
            
            File.Move(Path.Combine(this.SyncPath, "PlanTaskList.xml"), Path.Combine(newPath, "PlanTaskList.xml"));
            this.SyncPath = newPath;
            
            XmlElement nc = (XmlElement)_ConfigXml.SelectSingleNode("//PlanTaskManagerConfig//SyncPath");
            if (nc != null)
            {
                if (newPath.Equals(AppDomain.CurrentDomain.BaseDirectory))
                {
                    nc.InnerText = ".";
                }
                nc.InnerText = newPath;
            }
            _ConfigXml.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PlanTaskManagerConfig.xml"));
        }
    }
}
